//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.moons.settings.config;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;

class ARHidDevHelper {
    private UsbManager mUsbManager;
    private UsbDevice mUsbDevice;
    private UsbInterface mUsbInterface;
    private UsbDeviceConnection mConnection;
    private UsbEndpoint mUsbEndpointRead;
    private UsbEndpoint mUsbEndpointWrite;
    public static final String TAG = ARHidDevHelper.class.getSimpleName();

    public ARHidDevHelper() {
    }

    public Boolean isOpend() {
        return this.mConnection != null && this.mConnection.getFileDescriptor() != -1 ? true : false;
    }

    public Boolean OpenDevice(UsbManager manager, UsbDevice device) {
        this.CloseDevice();
        if (manager == null) {
            Log.i(TAG, "manager is null");
            return false;
        } else if (device == null) {
            Log.i(TAG, "device is null");
            return false;
        } else {
            this.mUsbManager = manager;
            this.mUsbDevice = device;
            this.mUsbInterface = null;
            Log.i(TAG, String.format("interface num: %d", device.getInterfaceCount()));

            int epi;
            for(epi = 0; epi < device.getInterfaceCount(); ++epi) {
                UsbInterface iface = device.getInterface(epi);
                if (iface.getInterfaceClass() == 3) {
                    Log.i(TAG, String.format("Get HID interface"));
                    this.mUsbInterface = iface;
                }
            }

            if (this.mUsbInterface == null) {
                return false;
            } else {
                for(epi = 0; epi < this.mUsbInterface.getEndpointCount(); ++epi) {
                    UsbEndpoint mEndpoint = this.mUsbInterface.getEndpoint(epi);
                    if (mEndpoint.getDirection() == 128) {
                        Log.i(TAG, String.format("Get IN endpoint"));
                        this.mUsbEndpointRead = mEndpoint;
                    } else if (mEndpoint.getDirection() == 0) {
                        Log.i(TAG, String.format("Get OUT endpoint"));
                        this.mUsbEndpointWrite = mEndpoint;
                    }
                }

                this.mConnection = this.mUsbManager.openDevice(this.mUsbDevice);
                if (this.mConnection == null) {
                    Log.i(TAG, "Open usb device failed");
                    return false;
                } else {
                    Log.i(TAG, "Open usb device success");
                    Boolean bret = this.mConnection.claimInterface(this.mUsbInterface, true);
                    if (bret) {
                        Log.i(TAG, "Calim interface success");
                        return true;
                    } else {
                        Log.i(TAG, "Calim interface failed");
                        this.mConnection.close();
                        return false;
                    }
                }
            }
        }
    }

    public void CloseDevice() {
        if (this.isOpend()) {
            this.mConnection.releaseInterface(this.mUsbInterface);
            this.mConnection.close();
        }
    }

    public int WriteData(byte[] data, int size) {
        if (this.isOpend() && size >= data.length) {
            int ret = this.mConnection.bulkTransfer(this.mUsbEndpointWrite, data, size, 200);
            return ret;
        } else {
            return -1;
        }
    }

    public int WriteData2(byte[] data, int size, int trytimes) {
        if (this.mConnection != null && trytimes >= 0) {
            int ti = 0;
            int ret = -1;

            while(true) {
                ++ti;
                if (ti == trytimes) {
                    break;
                }

                ret = this.mConnection.bulkTransfer(this.mUsbEndpointWrite, data, size, 200);
                if (ret == size) {
                    break;
                }

                Log.i(TAG, String.format("bulk transfer try: %d", ti));
            }

            return ret;
        } else {
            return -2;
        }
    }

    public int ReadData(byte[] data) {
        if (!this.isOpend()) {
            return -1;
        } else {
            int ret = this.mConnection.bulkTransfer(this.mUsbEndpointRead, data, data.length, 200);
            return ret;
        }
    }
}
